1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.io;
18
19 import static com.google.common.base.Charsets.UTF_8;
20
21 import java.io.FilterWriter;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25
26
27
28
29
30
31 public class TestCharSink extends CharSink implements TestStreamSupplier {
32
33 private final TestByteSink byteSink;
34
35 public TestCharSink(TestOption... options) {
36 this.byteSink = new TestByteSink(options);
37 }
38
39 public String getString() {
40 return new String(byteSink.getBytes(), UTF_8);
41 }
42
43 @Override
44 public boolean wasStreamOpened() {
45 return byteSink.wasStreamOpened();
46 }
47
48 @Override
49 public boolean wasStreamClosed() {
50 return byteSink.wasStreamClosed();
51 }
52
53 @Override
54 public Writer openStream() throws IOException {
55
56 return new FilterWriter(new OutputStreamWriter(byteSink.openStream(), UTF_8)) {
57 @Override
58 public void write(int c) throws IOException {
59 super.write(c);
60 flush();
61 }
62
63 @Override
64 public void write(char[] cbuf, int off, int len) throws IOException {
65 super.write(cbuf, off, len);
66 flush();
67 }
68
69 @Override
70 public void write(String str, int off, int len) throws IOException {
71 super.write(str, off, len);
72 flush();
73 }
74 };
75 }
76 }